package com.my.oschina.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.my.oschina.R;
import com.my.oschina.app.AppContext;
import com.my.oschina.app.AppException;
import com.my.oschina.bean.User;
import com.my.oschina.bean.UserInformation;
import com.my.oschina.common.StringUtils;
import com.my.oschina.common.UIHelper;

/**
 * 用户专页-用户资料
 */
public class UserInfoFragment extends SherlockFragment {

	private Layout layout;
	private User mUser;

	/**
	 * 返回FavoriteFragment实例
	 * 
	 * @param type
	 *            加载数据的类型
	 * @return FavoriteFragment实例
	 */
	public static UserInfoFragment newInstance(int uid, int hisuid,
			String hisname) {
		UserInfoFragment ff = new UserInfoFragment();
		Bundle bundle = new Bundle();
		bundle.putInt("uid", uid);
		bundle.putInt("hisuid", hisuid);
		bundle.putString("hisname", hisname);
		ff.setArguments(bundle);
		return ff;
	}

	public int getArgumentsUid() {
		return getArguments().getInt("uid");
	}

	public int getArgumentsHisuid() {
		return getArguments().getInt("hisuid");
	}

	public String getArgumentsHisname() {
		return getArguments().getString("hisname");
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		// outState.putSerializable("bean", bean);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.userinfofragment_layout,
				container, false);
		layout = new Layout();
		layout.face = (ImageView) view.findViewById(R.id.user_center_userface);
		layout.face.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});

		layout.username = (TextView) view.findViewById(R.id.user_center_name);
		layout.gender = (TextView) view.findViewById(R.id.user_center_gender);
		layout.from = (TextView) view.findViewById(R.id.user_center_from);
		layout.join = (TextView) view.findViewById(R.id.user_center_jointime);
		layout.devplatform = (TextView) view
				.findViewById(R.id.user_center_devplatform);
		layout.expertise = (TextView) view
				.findViewById(R.id.user_center_expertise);
		layout.latest = (TextView) view
				.findViewById(R.id.user_center_latestonline);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		new GetUserInfoTask().execute();
	}

	/**
	 * 加载数据AsyncTask
	 */
	private class GetUserInfoTask extends
			AsyncTask<Void, Void, UserInformation> {

		@Override
		protected UserInformation doInBackground(Void... params) {
			try {
				return ((AppContext) getSherlockActivity().getApplication())
						.getInformation(getArgumentsUid(),
								getArgumentsHisuid(), getArgumentsHisname(), 0,
								true);
			} catch (AppException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(UserInformation result) {
			super.onPostExecute(result);

			// 加载成功且如果有数据
			if (result != null && result.getUser() != null) {
				mUser = result.getUser();
				layout.username.setText(mUser.getName());
				layout.gender.setText(mUser.getGender());
				layout.from.setText(mUser.getLocation());
				layout.join.setText(StringUtils.friendly_time(mUser
						.getJointime()));
				layout.devplatform.setText(mUser.getDevplatform());
				layout.expertise.setText(mUser.getExpertise());
				layout.latest.setText(StringUtils.friendly_time(mUser
						.getLatestonline()));
				// 加载用户头像
				UIHelper.showUserFace(layout.face, mUser.getFace());
			}
			// 加载失败或者没有数据
			else {

			}
		}
	}

	private class Layout {
		ImageView face;
		TextView username;
		TextView gender;
		TextView from;
		TextView join;
		TextView devplatform;
		TextView expertise;
		TextView latest;
	}

}
